package com.hcc.repository.core.utils;

import java.lang.reflect.Field;

/**
 * MethodUtils
 *
 * @author hushengjun
 * @date 2023/10/31
 */
public class MethodUtils {

    public static String getGetterMethodName(Field field) {
        String fieldName = field.getName();
        String firstCharToUpperCase = StrUtils.firstCharToUpperCase(fieldName);
        if (boolean.class.equals(field.getType())) {
            // isSuccess -> isSuccess
            if (fieldName.startsWith("is")) {
                if (fieldName.length() > 2) {
                    String sub = fieldName.substring(2);
                    char firstChar = sub.charAt(0);
                    if (firstChar >= 'A' && firstChar <= 'Z') {
                        return fieldName;
                    }
                }
            }
            return "is" + firstCharToUpperCase;
        }

        return "get" + firstCharToUpperCase;
    }

    public static String getSetterMethodName(Field field) {
        String fieldName = field.getName();
        String firstCharToUpperCase = StrUtils.firstCharToUpperCase(fieldName);
        if (boolean.class.equals(field.getType())) {
            // isSuccess -> setSuccess
            if (fieldName.startsWith("is")) {
                if (fieldName.length() > 2) {
                    String sub = fieldName.substring(2);
                    char firstChar = sub.charAt(0);
                    if (firstChar >= 'A' && firstChar <= 'Z') {
                        firstCharToUpperCase = StrUtils.firstCharToUpperCase(sub);
                    }
                }
            }
        }

        return "set" + firstCharToUpperCase;
    }

}
